package com.ruijie.autotest.log.impl.parse;

import com.minelx.landisk.filesystem.core.File;
import com.ruijie.autotest.log.core.data.ITable;
import com.ruijie.autotest.log.impl.data.SimpleTable;

import java.io.IOException;

public class ColumnsAndValuesParser {

	private final IColumnsParser columnsParser;

	private final LinesParser linesParser;

	private final ICellsParser cellsParser;

	public ColumnsAndValuesParser(IColumnsParser columnsParser, LinesParser linesParser, ICellsParser cellsParser) {
		this.columnsParser = columnsParser;
		this.cellsParser = cellsParser;
		this.linesParser = linesParser;
	}

	public ITable parseTable(File columnsFile, File valuesFile) {
		try {
			SimpleTable.Builder result = new SimpleTable.Builder(
					columnsParser.parseColumns(columnsFile));
			for (String line : linesParser.parseLines(valuesFile)) {
				result.addRow(cellsParser.parseCells(line));
			}
			return result.build();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}
